project(stlink C)
cmake_minimum_required(VERSION 2.8.7)

include(CheckCCompilerFlag)
find_package(PkgConfig)

set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_SOURCE_DIR}/cmake/modules")

find_package(LibUSB REQUIRED)
pkg_check_modules(gtk gtk+-3.0)

function(add_cflag_if_supported flag)
    string(REPLACE "-" "_" flagclean ${flag})
    string(REPLACE "=" "_" flagclean ${flagclean})
    string(REPLACE "+" "_" flagclean ${flagclean})
    string(REPLACE "," "_" flagclean ${flagclean})
    string(TOUPPER ${flagclean} flagclean)

    check_c_compiler_flag(${flag} C_SUPPORTS${flagclean})

    if (C_SUPPORTS${flagclean})
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
    endif()
endfunction()

add_cflag_if_supported("-std=gnu99")
add_cflag_if_supported("-Wall")
add_cflag_if_supported("-Wextra")
add_cflag_if_supported("-Wshadow")
add_cflag_if_supported("-D_FORTIFY_SOURCE=2")
add_cflag_if_supported("-fstrict-aliasing")
add_cflag_if_supported("-Wformat")
add_cflag_if_supported("-Wformat-security")
add_cflag_if_supported("-Wmaybe-uninitialized")
add_cflag_if_supported("-Wmissing-variable-declarations")
add_cflag_if_supported("-Wshorten-64-to-32")
add_cflag_if_supported("-Wimplicit-function-declaration")
add_cflag_if_supported("-Wredundant-decls")
add_cflag_if_supported("-Wundef")
add_cflag_if_supported("-fPIC")

if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
	include(CTest)
	add_cflag_if_supported("-ggdb")
	add_cflag_if_supported("-O0")
elseif()
	add_cflag_if_supported("-O2")
endif()

set(STLINK_HEADERS
	include/stlink.h
	include/stlink/usb.h
	include/stlink/sg.h
	include/stlink/logging.h
	include/stlink/mmap.h
	include/stlink/chipid.h
)

set(STLINK_SOURCE
	src/chipid.c
	src/common.c
	src/usb.c
	src/sg.c
	src/logging.c
)

include_directories(${LIBUSB_INCLUDE_DIR})
include_directories(include)
include_directories(src/mingw)

add_library(${PROJECT_NAME} STATIC
            ${STLINK_HEADERS} # header files for ide projects generated by cmake
            ${STLINK_SOURCE})
target_link_libraries(${PROJECT_NAME} ${LIBUSB_LIBRARIES})

add_executable(st-flash src/tools/flash.c)
target_link_libraries(st-flash ${PROJECT_NAME})

add_executable(st-info src/tools/info.c)
target_link_libraries(st-info ${PROJECT_NAME})

add_executable(st-util src/gdbserver/gdb-remote.c
                       src/gdbserver/gdb-remote.h
                       src/gdbserver/gdb-server.c
                       src/gdbserver/gdb-server.h)
target_link_libraries(st-util ${PROJECT_NAME})

install(TARGETS ${PROJECT_NAME} st-flash st-util st-info
        RUNTIME DESTINATION bin
        ARCHIVE DESTINATION lib
)

if(NOT MINGW)
  list(APPEND STLINK_SOURCE src/tools/term.c)
  add_executable(st-term src/tools/term.c)
  target_link_libraries(st-term ${PROJECT_NAME})

  install(TARGETS st-term
          RUNTIME DESTINATION bin)
endif()

if(gtk_FOUND)
  include_directories(${gtk_INCLUDE_DIRS})
  set(GUI_SOURCES src/tools/gui/stlink-gui.c
                  src/tools/gui/stlink-gui.h)

  add_executable(stlink-gui-local ${GUI_SOURCES})
  set_target_properties(stlink-gui-local PROPERTIES
                        COMPILE_FLAGS -DSTLINK_UI_DIR=\\"${CMAKE_CURRENT_SOURCE_DIR}/gui\\")
  target_link_libraries(stlink-gui-local stlink ${gtk_LDFLAGS})

  set(INSTALLED_UI_DIR ${CMAKE_INSTALL_PREFIX}/share)
  add_executable(stlink-gui ${GUI_SOURCES})
  set_target_properties(stlink-gui PROPERTIES
                        COMPILE_FLAGS -DSTLINK_UI_DIR=\\"${INSTALLED_UI_DIR}\\")
  target_link_libraries(stlink-gui stlink ${gtk_LDFLAGS})

  install(TARGETS stlink-gui
          RUNTIME DESTINATION bin)
  install(FILES src/tools/gui/stlink-gui.ui
          DESTINATION ${INSTALLED_UI_DIR})
endif()
